package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.DeptZhuanjia;
import com.ruoyi.system.domain.ZhuanjiaBiandongDetail;
import com.ruoyi.system.domain.ZhuanjiaBiandongHistory;
import com.ruoyi.system.mapper.DeptZhuanjiaMapper;
import com.ruoyi.system.service.*;
import com.ruoyi.system.tools.BooleanMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 单位专家关联Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-08
 */
@Service
public class DeptZhuanjiaServiceImpl implements IDeptZhuanjiaService {
    @Autowired
    private DeptZhuanjiaMapper deptZhuanjiaMapper;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IZhuanjiaBiandongDetailService zhuanjiaBiandongDetailService;

    @Autowired
    private IZhuanjiaBiandongHistoryService zhuanjiaBiandongHistoryService;

    @Autowired
    private IDeptidOlddeptidService deptidOlddeptidService;

    /**
     * 查询单位专家关联
     *
     * @param id 单位专家关联主键
     * @return 单位专家关联
     */
    @Override
    public DeptZhuanjia selectDeptZhuanjiaById(String id) {
        return deptZhuanjiaMapper.selectDeptZhuanjiaById(id);
    }

    /**
     * 查询单位专家关联列表
     *
     * @param deptZhuanjia 单位专家关联
     * @return 单位专家关联
     */
    @Override
    public List<DeptZhuanjia> selectDeptZhuanjiaList(DeptZhuanjia deptZhuanjia) {
        return deptZhuanjiaMapper.selectDeptZhuanjiaList(deptZhuanjia);
    }

    /**
     * 新增单位专家关联
     *
     * @param deptZhuanjia 单位专家关联
     * @return 结果
     */
    @Override
    public int insertDeptZhuanjia(DeptZhuanjia deptZhuanjia) {
        return deptZhuanjiaMapper.insertDeptZhuanjia(deptZhuanjia);
    }

    /**
     * 修改单位专家关联
     *
     * @param deptZhuanjia 单位专家关联
     * @return 结果
     */
    @Override
    public int updateDeptZhuanjia(DeptZhuanjia deptZhuanjia) {
        return deptZhuanjiaMapper.updateDeptZhuanjia(deptZhuanjia);
    }

    /**
     * 批量删除单位专家关联
     *
     * @param ids 需要删除的单位专家关联主键
     * @return 结果
     */
    @Override
    public int deleteDeptZhuanjiaByIds(String ids) {
        return deptZhuanjiaMapper.deleteDeptZhuanjiaByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除单位专家关联信息
     *
     * @param id 单位专家关联主键
     * @return 结果
     */
    @Override
    public int deleteDeptZhuanjiaById(String id) {
        return deptZhuanjiaMapper.deleteDeptZhuanjiaById(id);
    }

    @Override
    public DeptZhuanjia findByZhuanjiaId(String ppZhuanjiaId) {
        DeptZhuanjia mmDeptzhuanjia = new DeptZhuanjia();
        mmDeptzhuanjia.setZhuanjiaid(ppZhuanjiaId);
        mmDeptzhuanjia.setIsvalid((long) 1);
        return selectDeptZhuanjiaList(mmDeptzhuanjia).size() == 1 ? selectDeptZhuanjiaList(mmDeptzhuanjia).get(0) : null;
    }

    @Transactional
    @Override
    public BooleanMessage zhuanyi(String ppZhuanjiaIds, String ppDeptId) {

        boolean ok = true;

        String[] zhuanjiaids = ppZhuanjiaIds.split(",");
        for (int i = 0; i < zhuanjiaids.length; i++) {
            String mmZhuanjiaId = zhuanjiaids[i];

            //查询旧的关系
            DeptZhuanjia mmDeptZhuanjia = new DeptZhuanjia();
            mmDeptZhuanjia.setZhuanjiaid(mmZhuanjiaId);
            mmDeptZhuanjia.setIsvalid((long) 1);

            List<DeptZhuanjia> mmList = this.selectDeptZhuanjiaList(mmDeptZhuanjia);
            if (mmList.size() != 1) {
                return BooleanMessage.getErrorMessage("该单位专家查询出错");
            }
            DeptZhuanjia tempDeptZhuanjia = mmList.get(0);

            if (ppDeptId.equals(tempDeptZhuanjia.getDeptid())) {
                return BooleanMessage.getErrorMessage("不可转移至原单位");
            }

            tempDeptZhuanjia.setIsvalid((long)2);
            tempDeptZhuanjia.setBiangengshijian(new Date());

            if (updateDeptZhuanjia(tempDeptZhuanjia) != 1) {
                ok = false;
                return BooleanMessage.getErrorMessage(tempDeptZhuanjia.getXingming() + "修改旧关系失败");
            }

            DeptZhuanjia mmNewDeptZhuanjia = new DeptZhuanjia();
            mmNewDeptZhuanjia.setId(UUID.randomUUID().toString());
            mmNewDeptZhuanjia.setZhuanjiaid(mmZhuanjiaId);
            mmNewDeptZhuanjia.setDeptid(ppDeptId);
            mmNewDeptZhuanjia.setXingming(tempDeptZhuanjia.getXingming());
            mmNewDeptZhuanjia.setDanweimingcheng(deptService.selectDeptById(ppDeptId).getJigoumingcheng());
            mmNewDeptZhuanjia.setBiangengshijian(new Date());
            mmNewDeptZhuanjia.setIsvalid((long)1);

            if (insertDeptZhuanjia(mmNewDeptZhuanjia) != 1) {
                ok = false;
                return BooleanMessage.getErrorMessage(mmNewDeptZhuanjia.getXingming() + "指派单位失败");
            }

            ZhuanjiaBiandongDetail mmZhuanjiaBiandongDetail = new ZhuanjiaBiandongDetail();
            mmZhuanjiaBiandongDetail.setId(UUID.randomUUID().toString());
            mmZhuanjiaBiandongDetail.setDeptid(deptidOlddeptidService.OldDeptIdToDeptId(ppDeptId));
            mmZhuanjiaBiandongDetail.setZhuanjiaid(mmZhuanjiaId);
            mmZhuanjiaBiandongDetail.setZhuanjiamingcheng(tempDeptZhuanjia.getXingming());
            mmZhuanjiaBiandongDetail.setBiandongleixing("11");//正常（转入）
            mmZhuanjiaBiandongDetail.setBiandongriqi(new Date());
            mmZhuanjiaBiandongDetail.setIsused((long)0);
            mmZhuanjiaBiandongDetail.setZhuanzidanwei(tempDeptZhuanjia.getDeptid());
            mmZhuanjiaBiandongDetail.setZhuanzidanweimingcheng(deptService.selectDeptById(tempDeptZhuanjia.getDeptid()).getJigoumingcheng());
            mmZhuanjiaBiandongDetail.setZhuanzhidanwei(ppDeptId);
            mmZhuanjiaBiandongDetail.setZhuanzhidanweimingcheng(deptService.selectDeptById(ppDeptId).getJigoumingcheng());
            mmZhuanjiaBiandongDetail.setBaosongshijian(new Date());
            mmZhuanjiaBiandongDetail.setShenpishijian(new Date());
            mmZhuanjiaBiandongDetail.setBeizhu1("1");
            mmZhuanjiaBiandongDetail.setIsvalid((long)1);

            if (zhuanjiaBiandongDetailService.insertZhuanjiaBiandongDetail(mmZhuanjiaBiandongDetail) != 1) {
                ok = false;
                return BooleanMessage.getErrorMessage(mmNewDeptZhuanjia.getXingming() + "新单位添加变动失败");
            }

            ZhuanjiaBiandongHistory mmZhuanjiaBiandongHistory = new ZhuanjiaBiandongHistory();
            mmZhuanjiaBiandongHistory.setId(UUID.randomUUID().toString());
            mmZhuanjiaBiandongHistory.setZhuanjiaid(mmZhuanjiaId);
            mmZhuanjiaBiandongHistory.setOldzhuantai("10");
            mmZhuanjiaBiandongHistory.setNewzhuangtai("11");
            mmZhuanjiaBiandongHistory.setOlddanwei(deptService.selectDeptById(tempDeptZhuanjia.getDeptid()).getJigoumingcheng());
            mmZhuanjiaBiandongHistory.setNewdanwei(deptService.selectDeptById(ppDeptId).getJigoumingcheng());
            mmZhuanjiaBiandongHistory.setBiandongshijian(new Date());
            mmZhuanjiaBiandongHistory.setOrdernum(zhuanjiaBiandongHistoryService.getMaxOrdernum(mmZhuanjiaId)+1);
            mmZhuanjiaBiandongHistory.setIsvalid((long)1);

            if (zhuanjiaBiandongHistoryService.insertZhuanjiaBiandongHistory(mmZhuanjiaBiandongHistory) != 1) {
                ok = false;
                return BooleanMessage.getErrorMessage(mmNewDeptZhuanjia.getXingming() + "变动历史添加失败");
            }
        }

        return BooleanMessage.getSuccessMessage("转移成功");
    }

    /**
     * @param mmDeptZhuanjia
     * @return
     */
    @Override
    public List<DeptZhuanjia> selectDeptZhuanjiaListByDeptid(DeptZhuanjia mmDeptZhuanjia) {
        return deptZhuanjiaMapper.selectDeptZhuanjiaListByDeptid(mmDeptZhuanjia);
    }
}
